package com.syh.jn.scm.infrastructure.repository.impl.operation.project;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtStringUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.core.expction.DataModelException;
import com.syh.jn.scm.domain.operation.entity.project.OperationProject;
import com.syh.jn.scm.domain.operation.repository.project.OperationProjectRepository;
import com.syh.jn.scm.dto.operation.project.OperationProjectVo;
import com.syh.jn.scm.infrastructure.repository.model.base.ProjectModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Date:2018/7/13
 * Author:Clown
 * Desc;
 */
@Repository
public class OperationProjectRepositoryImpl extends AbstractAggregateRepository<OperationProject,ProjectModel,Long> implements OperationProjectRepository{


  @Override
  public Long add(OperationProject project) {
    //取到父类的目录树
    ProjectModel father = new ProjectModel();
    if (project.getSupId() != null) {
      father = super.hibernateTemplate.get(ProjectModel.class, project.getSupId());
      if (father == null) {
        throw new DataModelException("父级分类不存在");
      }
    }

    ProjectModel model = new ProjectModel();
    ExtBeanUtils.copyProperties(project, model);
    super.hibernateTemplate.save(model);
    StringBuffer catalogCodeStr = ExtStringUtils.isEmpty(father.getCatalogCode()) ? new StringBuffer() : new StringBuffer(father.getCatalogCode()).append("-");
    model.setCatalogCode(catalogCodeStr.append(model.getId()).toString());
    return model.getId();
  }

  @Override
  public void update(OperationProject project) {
    //取到父类的目录树
    ProjectModel father = new ProjectModel();
    if (project.getSupId() != null) {
      father = super.hibernateTemplate.get(ProjectModel.class, project.getSupId());
      if (father == null) {
        throw new DataModelException("父级分类不存在");
      }
    }
    ProjectModel model = super.hibernateTemplate.get(ProjectModel.class, project.getId());
    ExtBeanUtils.copyProperties(project, model);
    super.hibernateTemplate.update(model);
    StringBuffer catalogCodeStr = ExtStringUtils.isEmpty(father.getCatalogCode()) ? new StringBuffer() : new StringBuffer(father.getCatalogCode()).append("-");
    model.setCatalogCode(catalogCodeStr.append(model.getId()).toString());
  }

  @Override
  public void updateStatus(OperationProject project) {
    super.update(project);
    List<OperationProject> subProjects = Collections.EMPTY_LIST;
    if(project.getParents() != null && project.getParents().size() > 0)
      subProjects = project.getParents();
    if(project.getSubProjects() != null && project.getSubProjects().size() > 0)
      subProjects = project.getParents();

    subProjects.forEach(subProject ->{
      super.update(subProject);
    });
  }

  @Override
  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(ProjectModel.class, "project", null),
              "project2.name as supName"
      );
      FROM(getModelTableName(ProjectModel.class)+" project");
      LEFT_OUTER_JOIN(getModelTableName(ProjectModel.class)+" project2 on project.SUP_ID = project2.ID");
    }};
  }

  @Override
  protected Class getViewObject() {
    return OperationProjectVo.class;
  }

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    Map<String, String> params = new HashMap<>();
    params.put("id", "project.ID");
    params.put("createTimeStamp", "project.CREATE_TIME_STAMP");
    params.put("code", "project.CODE");
    params.put("name", "project.NAME");
    params.put("status", "project.STATUS");
    params.put("catalogCode", "project.CATALOG_CODE");
    params.put("businessEntityId","project.BUSINESS_ENTITY_ID");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return params.get(propertyName);
    });
    return parse;
  }
}
